﻿using System;
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using SelfService.Helpers;
using WalkingTec.Mvvm.Core;
using SelfService.ViewModel.SysClientVMs;
using SelfService.Model.Models;


namespace SelfService.Controllers
{
	public partial class SysClientController
    {
        [ActionDescription("获取配置信息")]
        [HttpGet]
        [Public]
        public async Task<Result<SysClientVm>> Get()
        {
            var data = await this.GetClientAsync();
            
            if (data == null)
            {
                return new Result<SysClientVm>()
                {
                    Code = 1,
                    Message = "配置文件不存在"
                };
            }
            return new Result<SysClientVm>()
            {
                Data = data
            };
        }
        
        [ActionDescription("获取配置信息")]
        [HttpGet("Version")]        
        [Public]
        public async Task<Result<SysClientVm>> Version([FromQuery] string version = "")
        {
            var data = await this.GetClientAsync();
            if (data == null)
            {
                return new Result<SysClientVm>()
                {
                    Code = 1,
                    Message = "配置文件不存在"
                };
            }
            if (data.Entity.Version != version)
            {
                // 更新版本号
                var client = await DC.Set<SysClient>().FirstOrDefaultAsync(e => e.ID == data.Entity.ID);
                if (client != null)
                {
                    client.Version = version;
                    client.UpdateTime = DateTime.Now;
                    await DC.SaveChangesAsync();
                }
            }
            return new Result<SysClientVm>()
            {
                Data = data
            };
        }

        [ActionDescription("获取服务器时间")]
        [HttpGet("GetDate")]
        public Result<DateTime> GetDate()
        {
            return new Result<DateTime>()
            {
                Data = DateTime.Now
            };
        }
    }
}
